package secondStage.part3.List集合.List集合三种遍历运用;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName: [ListTest]
 * @Description: List集合存储学生对象并遍历
 * @Author: [黄劲松]
 * @CreateDate: [2021-04-02]
 * @Version: [v1.0]
 */
//需求:创建一个存储学生对象的集合。存储3个学生对象，使用程序实现在控制台遍历该集合
public class ListTest {
    public static void main(String[] args) {
        //创建List对象
        List<Student> list = new ArrayList<Student>();
        //创建学生对象
        Student s1 = new Student("张三", 12);
        Student s2 = new Student("李四", 12);
        Student s3 = new Student("王五", 12);
        //添加元素到集合
        list.add(s1);
        list.add(s2);
        list.add(s3);
        //迭代器方式遍历
        Iterator<Student> it = list.iterator();
        while (it.hasNext()) {
            Student s = it.next();
            System.out.println(s.getName() + "," + s.getAge());
        }
        System.out.println("===========");
        //for循环遍历
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s.getName() + "," + s.getAge());
        }
        System.out.println("===========");
        //增强for 遍历输出
        for (Student s : list) {
            System.out.println(s.getName() + "," + s.getAge());
        }
    }
}
